
layui.use(['form','layer'], function () {

    var $ = layui.$, form = layui.form, layer=layui.layer;

    var positionList;
    var page = 1;
    var limit = 4;

    $(function () {
        LoginStatus();

        var userInfo = JSON.parse($.cookie('userInfo'));
        $('#userName').text((userInfo.name == '' || userInfo.name ) ? "":userInfo.name);
        $('#userPosition').text(userInfo.position == '' ? '人事': userInfo.position);

        //加载公司信息
        loadCompany();
        loadResume();
        //加载职位信息
        loadPositionList(0);
        loadHrList();
        loadHrAuditList();
    })

    function loadResume() {
        $.ajax({
            type: 'get', // 提交方式 get/post
            url: companyServices+"/resume/delivery/count", // 需要提交的 url
            contentType:"application/json;charset=utf-8",
            xhrFields: {
                withCredentials: true
            }
            ,crossDomain: true
            ,data:{},
            success: function(res) {
                checkLoginStatus(res);

                if(res.status == '00200'){
                    /***

                    "all": 3,			#所有记录
                    "notViewed": 2,		#未查看
                    "wait": 1,			#查看了但未处理
                    "interview": 0,		#等待面试
                    "admitted": 0,		#已经录取
                    "obsolete": 0		#淘汰
                     */
                    $('#wait').text(res.data.wait);
                    $('#interview').text(res.data.interview);
                    $('#admitted').text(res.data.admitted);
                    $('#obsolete').text(res.data.obsolete);

                }
            }
        })
    }

    //加载公司信息
    function loadCompany() {
        $.ajax({
            type: 'get', // 提交方式 get/post
            url: companyServices+"/company", // 需要提交的 url
            contentType:"application/json;charset=utf-8",
            xhrFields: {
                withCredentials: true
            }
            ,crossDomain: true
            ,data:{},
            success: function(res) {
                checkLoginStatus(res);

                if(res.status == '00200'){
                    var companyInfo = res.data;
                    //$.cookie("companyInfo", JSON.stringify(companyInfo));
                    $('#companyLogo').attr('src', companyServices + companyInfo.logo);
                    $('#companyFullName').text((companyInfo.fullName == '' || companyInfo.fullName == null) ? "":companyInfo.fullName);
                    //$('#companySimpleName').text(companyInfo.simpleName);
                    $('#industry').text((companyInfo.industryName == '' || companyInfo.industryName == null) ? "" : companyInfo.industryName);
                    $('#companyLevel').text((companyInfo.companySizeLevelName == '' || companyInfo.companySizeLevelName == null) ? "":companyInfo.companySizeLevelName);
                }
            }
        })
    }


    $('#more').click(function () {
        window.location.href="job-manage.html";
        //loadPositionList(1);
    })
    //加载职位列表
    function loadPositionList(type) {
        if (type == 1) {
            page = page + 1;
        }
        layer.load(2);
        $.ajax({
            type: 'get', // 提交方式 get/post
            url: companyServices+"/position/list?page="+page+"&limit=" + limit, // 需要提交的 url
            contentType:"application/json;charset=utf-8",
            xhrFields: {
                withCredentials: true
            }
            ,crossDomain: true
            ,data:{},
            success: function(res) {
                layer.closeAll('loading');
                checkLoginStatus(res);

                if(JSON.parse(res).code == '0'){
                    positionList = JSON.parse(res).data;

                    var html = '';
                    if (positionList != null && positionList.length > 0) {
                        $.each(positionList, function (index, item) {
                            html += '<ul> <li>';
                            html += '<a href="wrap-edit-position.html?id='+item.id+'"><span>'+item.positionName+'</span>';
                            html += '<img src="../images/mobile/edit.png" alt="编辑">';
                            html += '</a> </li> <li><p>'+dateFormat("yyyy-MM-dd",new Date(item.updateTime))+'</p></li>';
                            html += '<li><span>'+((item.workCity == "" || item.workCity == null) ? "":item.workCity)+' | '+((item.workAddress == ""||item.workAddress == null)?"":item.workAddress)+' | '+((item.workExperienceLevelName == ""|| item.workExperienceLevelName == null)?"":item.workExperienceLevelName)+' | '+((item.educationLevelName == "" || item.educationLevelName == null ) ? "":item.educationLevelName)+'</span></li>';
                            html += '<li><i>'+((item.salaryLevelName == "" || item.salaryLevelName == null) ? "":item.salaryLevelName)+'</i></li>';
                            html += '</ul>';
                        })

                        if (type == 1) {
                            $('#positionList').append(html);
                        } else {
                            $('#positionList').html(html);
                        }


                    }
                }
            }, fail:function () {
                layer.closeAll('loading');
            }
        })
    }
    window.delHr = function(hrId) {
        layer.confirm('是否确认删除该hr？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            delPosition(hrId)
        }, function(){

        });
    }

    function delPosition(hrId) {
        layer.load(2);
        $.ajax({
            type: 'delete', // 提交方式 get/post
            url: companyServices+"/hr/" + hrId, // 需要提交的 url
            contentType:"application/json;charset=utf-8",
            xhrFields: {
                withCredentials: true
            }
            ,crossDomain: true
            ,data: {}
            ,success: function(res) {
                layer.closeAll('loading');
                if(res.status == '00200'){
                    layerMsg(res.message);
                    setTimeout(function() {
                        window.location.href="wrap-company-backstage.html" ;
                    }, 2000);

                }else{
                    layerMsg(res.message)
                }
            },fail:function () {
                layer.closeAll('loading');
            }
        })
    }

    //加载职位列表
    function loadHrList() {
        //layer.load(2);
        $.ajax({
            type: 'get', // 提交方式 get/post
            url: companyServices+"/hr/list?page=1&limit=20", // 需要提交的 url
            contentType:"application/json;charset=utf-8",
            xhrFields: {
                withCredentials: true
            }
            ,crossDomain: true
            ,data:{},
            success: function(res) {
                //layer.closeAll('loading');
                checkLoginStatus(res);

                if(res.status == '00200'){
                    var hrList = res.data;

                    var html = '';
                    if (hrList != null && hrList.length > 0) {
                        $.each(hrList, function (index, item) {
                            html += '<ul> <li> <a href="javascript:;">';
                                html += '<span>'+((item.userName == "" || item.userName == null) ? "":item.userName)+' | '+((item.userPosition == "" || item.userPosition == null) ? "":item.userPosition)+'</span>';
                                html += '<img src="../images/mobile/delete.png" alt="" onclick="delHr(\''+item.userId+'\')"> </a>';
                                html += '</li> <li><span><img src="../images/phone.png" alt="电话"></span>'+((item.userTel == "" || item.userTel == null) ? "":item.userTel)+'</li> <li><span><img src="../images/mailbox.png" alt="邮箱"></span>'+((item.userEmail == "" || item.userEmail == null) ? "":item.userEmail)+'</li> </ul>';
                        })

                        $('#hrList').html(html);

                    }
                }
            }, fail:function () {
                //layer.closeAll('loading');
            }
        })
    }

    window.pass = function(id) {
        layer.confirm('确定同意该HR的申请吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            handleHr(id, 1);
        }, function(){

        });
    }

    window.noPass = function(id) {
        layer.confirm('确定驳回该HR的申请吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            handleHr(id, 0);
        }, function(){

        });
    }

    function handleHr(id, type) {
        var url = '';
        if (type == 1) {
            url = companyServices+"/hr/audit/admit/" + id;
        } else {
            url = companyServices+"/hr/audit/reject/" + id;
        }

        layer.load(2);
        $.ajax({
            type: 'post', // 提交方式 get/post
            url:url,
            contentType:"application/json;charset=utf-8",
            xhrFields: {
                withCredentials: true
            }
            ,crossDomain: true
            ,data: {}
            ,success: function(res) {
                layer.closeAll('loading');

                checkLoginStatus(res);

                if(res.status == '00200'){
                    layerMsg(res.message);
                    setTimeout(function() {
                        window.location.href="wrap-company-backstage.html" ;
                    }, 2000);

                }else{
                    layerMsg(res.message)
                }
            },fail:function () {
                layer.closeAll('loading');
            }
        })
    }

    function loadHrAuditList() {
        //layer.load(2);
        $.ajax({
            type: 'get', // 提交方式 get/post
            url: companyServices+"/hr/audit/list?page=1&limit=20", // 需要提交的 url
            contentType:"application/json;charset=utf-8",
            xhrFields: {
                withCredentials: true
            }
            ,crossDomain: true
            ,data:{},
            success: function(res) {
                //layer.closeAll('loading');
                checkLoginStatus(res);

                if(res.status == '00200'){
                    var hrAuditList = res.data;

                    var html = '';
                    if (hrAuditList != null && hrAuditList.length > 0) {
                        $.each(hrAuditList, function (index, item) {
                            html += '<ul><li>';
                            html += '<a href="#">';
                            html += '<span>'+((item.hrName == "" || item.hrName == null) ? "":item.hrName)+' | '+((item.hrPosition == "" || item.hrPosition == null) ? "":item.hrPosition)+'</span>';
                            html += '<div>'
                            html += '<img src="../images/pass.png" class="pass-hr-btn" onclick="pass(\''+item.id+'\')">';
                            html += '<img src="../images/mobile/delete.png" class="delete-hr-btn" onclick="noPass(\''+item.id+'\')">';
                            html += '</div>'
                            html += '</a> </li> ';
                            html +='<li><span><img src="../images/phone.png" alt="电话"></span>'+((item.hrTel == "" || item.hrTel == null) ? "":item.hrTel)+'</li>';
                            html += '<li><span><img src="../images/mailbox.png" alt="邮箱"></span>'+((item.hrEmail == "" || item.hrEmail == null) ? "":item.hrEmail)+'</li></ul>';
                        })

                        $('#hrAuditList').html(html);

                    }
                }
            }, fail:function () {
                //layer.closeAll('loading');
            }
        })
    }




    /**************************************时间格式化处理************************************/
    function dateFormat(fmt,date) {
        var o = {
            "M+" : date.getMonth()+1,                 //月份
            "d+" : date.getDate(),                    //日
            "h+" : date.getHours(),                   //小时
            "m+" : date.getMinutes(),                 //分
            "s+" : date.getSeconds(),                 //秒
            "q+" : Math.floor((date.getMonth()+3)/3), //季度
            "S"  : date.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt))
            fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
        for(var k in o)
            if(new RegExp("("+ k +")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        return fmt;
    }
});




